package com.tianpengcc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tianpengcc.model.DateType;
import com.tianpengcc.model.Diray;
import com.tianpengcc.model.PageBean;
import com.tianpengcc.model.custom.DirayTypeCustom;
import com.tianpengcc.service.DirayService;
import com.tianpengcc.service.DirayTypeService;
import com.tianpengcc.service.impl.DirayServiceImpl;
import com.tianpengcc.service.impl.DirayTypeServiceImpl;
import com.tianpengcc.util.PropertiesUtil;

public class MainServlet extends HttpServlet {
	
	private DirayService dirayService = new DirayServiceImpl() ;
	
	private DirayTypeService dirayTypeService = new DirayTypeServiceImpl() ;
	
	/**
	 * <p>Description:</p>
	 * <p>Compary</p>
	 * @author 田鹏
	 * @date 2017年10月24日下午10:19:50
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response) ;
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String page = request.getParameter("page");
			if(page == null){
				page = "1" ; 
			}
			int pageNumber = Integer.parseInt(page) ;
			int pageSize = Integer.parseInt(PropertiesUtil.getValue("pageSize")) ;
			
			String f_dirayTypeId = request.getParameter("f_dirayTypeId");
			String year = request.getParameter("year");
			String month = request.getParameter("month") ;
			
			String f_dirayName = request.getParameter("f_dirayName") ;
 			
			System.out.println(f_dirayName);
			
			PageBean<Diray> pageBean = null ;
			if(f_dirayTypeId != null && !"".equals(f_dirayTypeId)){
				pageBean = dirayService.getDirayPageBeanByDirayTypeIdAndLimit(Integer.parseInt(f_dirayTypeId), pageNumber, pageSize) ;
			}else if(year != null && !"".equals(year) && month != null && !"".equals(month)){
				pageBean = dirayService.getByYearAndMonth(Integer.parseInt(year), Integer.parseInt(month), pageNumber, pageSize) ;
			}else if(f_dirayName != null && !"".equals(f_dirayName)){
				pageBean = dirayService.getByDirayNameLikeAndLimit(f_dirayName, pageNumber, pageSize) ;
			}else{
				pageBean = dirayService.getDirayPageBeanByLimit(pageNumber, pageSize);
			}
			
			System.out.println(pageBean.getTotalPage());
			
			List<DirayTypeCustom> dirayTypeList = dirayTypeService.getAll();
			
			int dateTypeSize = Integer.parseInt(PropertiesUtil.getValue("dateTypeSize")) ;
			List<DateType> dateTypeList = dirayService.getDateTypeBySize(dateTypeSize);
			
			request.setAttribute("f_dirayName", f_dirayName);
			request.setAttribute("year", year);
			request.setAttribute("month", month);
			request.setAttribute("f_dirayTypeId", f_dirayTypeId);
			request.setAttribute("dateTypeList", dateTypeList);
			request.setAttribute("dirayTypeList", dirayTypeList);
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("mainPage","/diray/dirayList.jsp");
			request.getRequestDispatcher("main.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			request.setAttribute("error", "访问失败500");
			request.getRequestDispatcher("/error.jsp").forward(request, response) ;
		}
	}

}
